#include "stdafx.h"

#if !defined(SOLIDWORKS_HEADERS)
#define SOLIDWORKS_HEADERS
// Solidworks includes
#include <amapp.h>
//#include <swpublished_i.h>
//#include <swpublished_i.c>
//#include <sldworks_i.c>
#include <swconst.h>
#endif
#include "TestSWX.h"

using namespace std;
using namespace CEEFIT;

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

extern CComPtr<ISldWorks> swApp;

namespace swxbatch_ceefit
{
	ceefit_init_spec TestSWX::TestSWX()
	{
		RegisterCeefitField(this, "sldprt_name", m_sldprt_name);
		RegisterCeefitTest(this, "nos_feats", &TestSWX::nos_feats);

		//cout << endl << "constructing";
	}

	ceefit_dtor_spec TestSWX::~TestSWX() {
		//cout << endl << "destructing";
	}

	double ceefit_call_spec TestSWX::nos_feats() {
		ExecutingPath();
		wstring wsFullPath;
		GetFullPath(wsFullPath);
		wcout << "\nfull path in nos_feats = " << wsFullPath.c_str() << endl;
		if( swApp ) {
			IModelDoc* swModel = NULL;
			if( swApp->put_Visible(FALSE) == S_OK ) {
				if( swApp->DocumentVisible(FALSE, swDocPART) == S_OK ) {
					CComBSTR sFileName(wsFullPath.c_str());
					CComBSTR sDefaultConfiguration(L"Default");
					long fileerror;
					if( swApp->IOpenDocSilent(sFileName.m_str, swDocPART, &fileerror, &swModel) == S_OK ) {
						if( swModel != NULL ) {
							long feat_count=-1;
							if( SUCCEEDED(swModel->GetFeatureCount(&feat_count)) )
								return int(feat_count);
						}
					}
				}
			}
		}
		VARIANT_BOOL ret;
		swApp->CloseAllDocuments(TRUE, &ret);
		return 0;
	}

	void TestSWX::ExecutingPath() {
		wchar_t sExecPath[MAX_PATH];
		GetModuleFileName(NULL, sExecPath, MAX_PATH);
		//wcout << endl << "executable = " << sExecPath;
	}

	void TestSWX::GetFullPath(wstring & wsFullPath) {
		wchar_t cFullPath[MAX_PATH] = TEXT("");
		LPTSTR  lpszFilePart = NULL;
		GetFullPathName(m_sldprt_name.GetBuffer(), MAX_PATH, cFullPath, & lpszFilePart);
		//wcout << endl << "full path = " << cFullPath;
		wsFullPath = cFullPath;
	}

	static REGISTERFIXTURECLASS< TestSWX > FatTableFixtureRegistration("swxbatch_ceefit::TestSWX", "AKA_TESTSWX"); 
} //namespace swxbatch_ceefit